package com.neusoft.service.impl;

import com.neusoft.mapper.PointMapper;
import com.neusoft.po.Point;
import com.neusoft.service.PointService;
import com.neusoft.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
public class PointServiceImpl implements PointService {

    @Autowired
    private PointMapper pointMapper;

    @Override
    public int savePoints(Point point) {
        point.setPurchaseDate(CommonUtil.getCurrentDate());
        return pointMapper.savePoints(point);
    }

    @Override
    public List<Point> checkPoints(String userId) {
        int period = 7;
        //1.获取积分情况
        List<Point> points = pointMapper.getPoints(userId);
        //2.根据积分情况判断是否有过期，过期便删除
        Iterator<Point> iterator = points.iterator();
        while (iterator.hasNext()) {
            Point point = iterator.next();
            String curDate = CommonUtil.getCurrentDate();
            String oldDate = point.getPurchaseDate();
            int result =CommonUtil.checkDate(curDate, oldDate,period);
            if (result == 0) {
                pointMapper.deletePoints(point);
                iterator.remove(); // 使用迭代器的remove()方法删除元素
            }else {
                point.setExpireRemain(result);
            }
        }
        //3.返回现在存在的积分情况
        return points;
    }


}
